package ru.bmstu.vks.client.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import java.awt.Toolkit;
import javax.swing.ImageIcon;

import ru.bmstu.vks.client.app.ClientManager;

public class LoginDialog extends JDialog {

	private static final long serialVersionUID = -4238645445114833083L;
	private final JPanel contentPanel = new JPanel();
	private JTextField login;
	private JButton cancel;
	private JButton submit;
	private JPasswordField password;
	
	private String loginS;
	private String passwordS;
	private JLabel lblNewLabel;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			LoginDialog dialog = new LoginDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public LoginDialog() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(LoginDialog.class.getResource("/data/icon.png")));
		setTitle("\u0417\u0430\u043F\u0440\u043E\u0441 \u0430\u0432\u0442\u043E\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043E\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430");
		setResizable(false);
		setModal(true);
		setBounds(100, 100, 297, 133);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel label = new JLabel("\u041F\u0430\u0440\u043E\u043B\u044C:");
			label.setBounds(68, 39, 82, 14);
			contentPanel.add(label);
		}
		{
			JLabel label = new JLabel("\u041B\u043E\u0433\u0438\u043D:");
			label.setBounds(68, 14, 82, 14);
			contentPanel.add(label);
		}
		{
			login = new JTextField();
			login.setColumns(10);
			login.setBounds(160, 11, 121, 20);
			contentPanel.add(login);
		}
		
		password = new JPasswordField();
		password.setBounds(160, 36, 121, 20);
		contentPanel.add(password);
		{
			lblNewLabel = new JLabel("");
			lblNewLabel.setIcon(new ImageIcon(LoginDialog.class.getResource("/data/authoriza.png")));
			lblNewLabel.setBounds(10, 14, 46, 39);
			contentPanel.add(lblNewLabel);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				submit = new JButton("\u0410\u0432\u0442\u043E\u0440\u0438\u0437\u0430\u0446\u0438\u044F");
				submit.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent arg0) {
					}
				});
				submit.addActionListener(new ActionListener() {
					@SuppressWarnings("deprecation")
					public void actionPerformed(ActionEvent e) {
						loginS = login.getText();
						passwordS = password.getText();
						LoginDialog.this.setVisible(false);
					}
				});
				submit.setActionCommand("OK");
				buttonPane.add(submit);
				getRootPane().setDefaultButton(submit);
			}
			{
				cancel = new JButton("\u041E\u0442\u043C\u0435\u043D\u0430");
				cancel.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						ClientManager.disconnectFromServer();
						LoginDialog.this.setVisible(false);
					}
				});
				cancel.setActionCommand("Cancel");
				buttonPane.add(cancel);
			}
		}
	}
	
	public String getLogin() {
		return loginS;
	}
	
	public String getPassword() {
		return passwordS;
	}
}
